from django.db import models
from django.contrib.auth.models import User
from iperscribo.utils import newUID

from iperscribo.pages.models import Page

OBJECT_TYPE = (
    ('0', Page),
)

ACTION_TYPE = (
    ('0', 'Create'),
    ('1', 'Update'),
    
)

class Project(models.Model):
    uid = models.CharField(max_length=40, default=newUID, primary_key=True)
    name = models.CharField(max_length=40)
    owner = models.ForeignKey(User)
    creation_date = models.DateTimeField(auto_now_add=True)
    
    def __unicode__(self):
        return self.name

#################################################################################################################################################

class Activity(models.Model):
    uid = models.CharField(max_length=40, default=newUID, primary_key=True)
    type = models.CharField(max_length=1, choices=OBJECT_TYPE)
    action = models.CharField(max_length=1, choices=ACTION_TYPE)
    user = models.ForeignKey(User)
    comment = models.CharField(max_length=200, default = '')
    action_date = models.DateTimeField(auto_now_add=True)
    object_uid = models.CharField(max_length=40)

    #############################################################################################################################################

    class Meta:
        verbose_name_plural = ('activities')

    #############################################################################################################################################
    
    def __unicode__(self):
        
        object = OBJECT_TYPE[int(self.type)][1].objects.get(uid = self.object_uid)
        return '[%s]:%s %s by user %s - %s' % (self.action_date.strftime("%a %d %b %Y %H:%M:%S"), object, ACTION_TYPE[int(self.action)][1], self.user, self.comment)

